package com.kaigao.demo.controllers;

import com.github.pagehelper.PageInfo;
import com.kaigao.demo.dao.model.SanGuoUserDo;
import com.kaigao.demo.dto.SanGuoUserDto;
import com.kaigao.demo.service.SanGuoUserService;
import com.kaigao.demo.utils.ResultModel;
import com.kaigao.demo.utils.ValidateException;
import com.kaigao.demo.vo.SanGuoUserVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName SanGuoUserController.java
 * @Description 功能描述： 控制层，这一层不要写太多业务逻辑，主要是处理请求
 * @author 吴昊 2017年9月7日 下午3:04:52
 */
@EnableAutoConfiguration
@RefreshScope
@RestController
public class SanGuoUserController {

    private static final Logger log = LoggerFactory.getLogger(SanGuoUserController.class);
    @Autowired
    private SanGuoUserService   sanGuoUserService;

    /**
     * @Description 方法描述： swagger-ui接口文档
     * @author 吴昊 2018年3月18日22:34:55
     * @return
     */
    @ApiIgnore
    @RequestMapping("/doc")
    public ModelAndView swaggerInfo() {

        String url = "redirect:swagger-ui.html";
        return new ModelAndView(url);
    }

    /**
     * @Description 方法描述： 首页
     * @author 吴昊 2018年3月18日22:35:23
     * @param request
     * @return
     */
    @ApiIgnore
    @RequestMapping("/")
    public ModelAndView index(HttpServletRequest request) {
        try {
            request.setAttribute("title", "三国人物");
            request.setAttribute("userList", sanGuoUserService.getList(null));
        } catch (ValidateException e) {
            log.warn(e.getMessage());
            return new ModelAndView("redirect:500.html");
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
            return new ModelAndView("redirect:500.html");
        }
        return new ModelAndView("index");
    }

    /**
     * @Description 方法描述： 新增：接口数据为map
     * @author 吴昊 2018年3月18日22:35:35
     * @return
     * @throws Exception
     * @throws ValidateException
     */
    @ApiOperation(value = "新增", notes = "根据传入对象新增信息")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResultModel<Boolean> add(SanGuoUserDo record) {
        ResultModel<Boolean> resultModel = new ResultModel<Boolean>();
        resultModel.setReturnValue(sanGuoUserService.add(record));
        return resultModel;
    }

    /**
     * @Description 方法描述： 批量逻辑删除
     * @author 吴昊 2018年3月18日22:35:43
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量逻辑删除", notes = "根据url中的id来指定删除对象")
    @ApiImplicitParam(name = "ids", value = "广告id,多个以英文半角逗号分开", required = true, paramType = "path", dataType = "String")
    @RequestMapping(value = "/delete/{ids}", method = RequestMethod.DELETE)
    public ResultModel<Boolean> delete(@PathVariable(value = "ids") String ids) {
        ResultModel<Boolean> resultModel = new ResultModel<Boolean>();
        resultModel.setReturnValue(sanGuoUserService.deletes(ids));
        return resultModel;
    }

    /**
     * @Description 方法描述： 修改
     * @author 吴昊 2018年3月18日22:35:51
     * @return
     */
    @ApiOperation(value = "修改", notes = "根据传入对象修改信息")
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public ResultModel<Boolean> update(SanGuoUserDo record) {

        ResultModel<Boolean> resultModel = new ResultModel<Boolean>();
        resultModel.setReturnValue(sanGuoUserService.update(record));
        return resultModel;
    }

    /**
     * @Description 方法描述： 根据条件查询列表
     * @author 吴昊 2018年3月18日22:35:59
     * @return
     */
    @ApiOperation(value = "根据条件查询列表", notes = "分页，默认10条每页。")
    @ApiImplicitParams({ @ApiImplicitParam(name = "pageNum", value = "页码", required = false, paramType = "query", dataType = "int"),
                         @ApiImplicitParam(name = "pageSize", value = "每页数量", required = false, paramType = "query", dataType = "int"),
                         @ApiImplicitParam(name = "orderParam", value = "排序字段，默认id", required = false, paramType = "query", dataType = "String"),
                         @ApiImplicitParam(name = "orderType", value = "排序方式：asc:顺序，desc：倒序，默认倒序", required = false, paramType = "query", dataType = "String"),
                         @ApiImplicitParam(name = "keywords", value = "查询关键字：广告标题", required = false, paramType = "query", dataType = "String"),
                         @ApiImplicitParam(name = "startTime", value = "创建时间开始,日期格式，如2017-08-08", required = false, paramType = "query", dataType = "date"),
                         @ApiImplicitParam(name = "endTime", value = "创建时间结束,如2017-08-08", required = false, paramType = "query", dataType = "date"),
                         @ApiImplicitParam(name = "sex", value = "性别： 0 男 1 女", required = false, paramType = "query", dataType = "int") })
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    public ResultModel<PageInfo<SanGuoUserVo>> getList(SanGuoUserDto sanGuoUserDto) {

        ResultModel<PageInfo<SanGuoUserVo>> resultModel = new ResultModel<PageInfo<SanGuoUserVo>>();
        resultModel.setReturnValue(sanGuoUserService.getList(sanGuoUserDto));
        return resultModel;

    }

    /**
     * @Description 方法描述：根据id获取详细信息
     * @author 吴昊 2018年3月18日22:36:08
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id获取详细信息", notes = "根据url中的id查询详情")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "path", dataType = "int")
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public ResultModel<SanGuoUserDo> getById(@PathVariable(value = "id") Integer id) {

        ResultModel<SanGuoUserDo> resultModel = new ResultModel<SanGuoUserDo>();
        resultModel.setReturnValue(sanGuoUserService.getById(id));
        return resultModel;

    }

    /**
     * @Description 方法描述： 根据多个id查询列表
     * @author 吴昊 2018年3月18日22:36:14
     * @param ids
     * @return
     */
    @ApiOperation(value = "根据多个id查询列表", notes = "根据url中的多个id查询对象")
    @ApiImplicitParam(name = "ids", value = "商品id,多个以英文半角逗号分开", required = true, paramType = "query", dataType = "String")
    @RequestMapping(value = "/getListByIds", method = RequestMethod.GET)
    public ResultModel<List<SanGuoUserVo>> getListByIds(@RequestParam(value = "ids", required = true) String ids) {

        ResultModel<List<SanGuoUserVo>> resultModel = new ResultModel<List<SanGuoUserVo>>();
        resultModel.setReturnValue(sanGuoUserService.getListByIds(ids));
        return resultModel;
    }

}
